<?php
	include_once("../includes/functions.php");

	foreach ($_POST as $key=>$value)
	{
		switch ($key)
		{
			case "qcategoryname":
				if (trim($value) == "")
					array_push($errors,array($key,"Please enter a category name"));
				break;
		}
	}
	
	if (sizeof($errors) > 0)
		echo displayErrors($errors);
	elseif (isset($_POST["submit"]))
	{
		if ($_POST["submit"] == "Yes")
		{
			if ($mode == "add")
			{
				$sql = "INSERT INTO qcategory(name) VALUES('".$_POST["qcategoryname"]."')";
				$addquestion = $conn1->Execute($sql);

				if (!$addquestion) {
					echo "SQL statement failed with error:\n";
				} else {
					echo "Question Category Successfully Added! Click <a href=\"../qcategory/viewqcategories.php?a=setup\">here</a> to continue";
				}  
			}
			else
			{
				$sql = "UPDATE qcategory SET name = '".$_POST['qcategoryname']."' WHERE id = '".$_GET['i']."'";
				$updatequestion = $conn1->Execute($sql);

				if (!$updatequestion) {
					echo "SQL statement failed with error:\n";
				} else {
					echo "Question Group Successfully Updated! Click <a href=\"../qcategory/viewqcategories.php?a=setup\">here</a> to continue";
				}  
			}			
		}
		elseif ($_POST["submit"] == "Submit")
			echo displayConfirmation();
	}
	
?>